package com.xkstream.client.ui;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.base.WatcherAdapter;
import com.xkstream.client.entity.User;
import com.xkstream.client.presenter.RegisterContractV2;
import com.xkstream.client.presenter.RegisterPresenter;
import com.xkstream.client.presenter.RegisterPresenterV2;
import com.xkstream.client.utils.DeviceUtils;
import com.xkstream.client.utils.MD5Util;
import com.xkstream.client.utils.Utils;
import com.xkstream.client.view.ResizeLayout;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017-9-23.
 */

public class RegisterActivity_V2 extends BaseActivity<RegisterContractV2.Presenter>
        implements RegisterContractV2.View {

    @BindColor(R.color.login_input_selected)
    int colorInputSelected;
    @BindColor(R.color.login_input_unselected)
    int colorInputUnselected;
    @BindView(R.id.edt_account)
    EditText edtAccount;
    @BindView(R.id.edt_pwd)
    EditText edtPwd;
    @BindView(R.id.root)
    ResizeLayout root;
    @BindView(R.id.edt_repwd)
    EditText edtRepwd;
    @BindView(R.id.edt_email)
    EditText edtEmail;
    @BindView(R.id.btn_register)
    TextView btnRegister;
    @BindView(R.id.tv_remembered)
    TextView tvRemembered;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register_v2);
        ButterKnife.bind(this);
        presenter = new RegisterPresenterV2(this);
        initListener();
    }

    @OnClick({R.id.btn_register, R.id.tv_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                register();
                break;
            case R.id.tv_login:
                jumpToActivity(LoginActivity_V2.class);
                break;
        }
    }

    private void initListener() {
        edtAccount.setOnFocusChangeListener((v, focus) -> {
            edtAccount.setCompoundDrawablesWithIntrinsicBounds(
                    focus ? R.drawable.nick_selected : R.drawable.nick_unselected,
                    0, 0, 0);
            edtAccount.setBackgroundResource(focus ? R.drawable.login_input_selected
                    : R.drawable.login_input_unselected);
            edtAccount.setTextColor(focus ? colorInputSelected : colorInputUnselected);
            edtAccount.setHintTextColor(focus ? colorInputSelected : colorInputUnselected);
        });

        edtPwd.setOnFocusChangeListener((v, focus) -> {
            edtPwd.setCompoundDrawablesWithIntrinsicBounds(
                    focus ? R.drawable.pwd_selected : R.drawable.pwd_unselected,
                    0, 0, 0);
            edtPwd.setBackgroundResource(focus ? R.drawable.login_input_selected
                    : R.drawable.login_input_unselected);
            edtPwd.setTextColor(focus ? colorInputSelected : colorInputUnselected);
            edtPwd.setHintTextColor(focus ? colorInputSelected : colorInputUnselected);
        });

        edtRepwd.setOnFocusChangeListener((v, focus) -> {
            edtRepwd.setCompoundDrawablesWithIntrinsicBounds(
                    focus ? R.drawable.pwd_selected : R.drawable.pwd_unselected,
                    0, 0, 0);
            edtRepwd.setBackgroundResource(focus ? R.drawable.login_input_selected
                    : R.drawable.login_input_unselected);
            edtRepwd.setTextColor(focus ? colorInputSelected : colorInputUnselected);
            edtRepwd.setHintTextColor(focus ? colorInputSelected : colorInputUnselected);
        });


        edtEmail.setOnFocusChangeListener((v, focus) -> {
            edtEmail.setCompoundDrawablesWithIntrinsicBounds(
                    focus ? R.drawable.email_selected : R.drawable.email_unselected,
                    0, 0, 0);
            edtEmail.setBackgroundResource(focus ? R.drawable.login_input_selected
                    : R.drawable.login_input_unselected);
            edtEmail.setTextColor(focus ? colorInputSelected : colorInputUnselected);
            edtEmail.setHintTextColor(focus ? colorInputSelected : colorInputUnselected);
        });

        edtAccount.addTextChangedListener(new WatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setBtnState();
            }
        });

        edtPwd.addTextChangedListener(new WatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setBtnState();
            }
        });

        edtEmail.addTextChangedListener(new WatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setBtnState();
            }
        });

        edtRepwd.addTextChangedListener(new WatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setBtnState();
            }
        });

        root.setOnKeyboardShowListener(new ResizeLayout.OnkeyboardShowListener() {
            @Override
            public void onKeyboardShow(int height) {
                int[] location = new int[2];
                tvLogin.getLocationOnScreen(location);
                int position = DeviceUtils.deviceHeight(RegisterActivity_V2.this) - location[1] - tvLogin.getHeight();
                root.setPadding(0, -(height - position), 0, 0);

            }

            @Override
            public void onKeyboardHide() {
                root.setPadding(0, 0, 0, 0);
            }
        });
    }

    private void setBtnState() {
        if (edtAccount.getText().length() >= 4 && edtPwd.getText().length() >= 4
                && edtPwd.getText().toString().equals(edtRepwd.getText().toString())
                && edtRepwd.getText().length() >= 4 &&
                Utils.checkEmail(edtEmail.getText().toString())) {
            btnRegister.setBackgroundResource(R.drawable.btn_clickable);
        } else {
            btnRegister.setBackgroundResource(R.drawable.btn_unclickable);
        }
    }

    private void register() {
        if (checkInput()) {
            Map<String, Object> params = new HashMap<>();
            params.put("user_name", edtAccount.getText().toString());
            params.put("password", MD5Util.string2MD5(edtPwd.getText().toString()));
            params.put("re_password", MD5Util.string2MD5(edtRepwd.getText().toString()));
            params.put("email", edtEmail.getText().toString());
            presenter.anchorRegister(params);
        }
    }


    private boolean checkInput() {
        if (edtAccount.getText().length() < 4) {
            showTopTips(R.string.account_length);
            return false;
        }
        if (edtPwd.getText().length() < 4) {
            showTopTips(R.string.pwd_length);
            return false;
        }
        if (!edtPwd.getText().toString()
                .equals(edtRepwd.getText().toString())) {
            showTopTips(R.string.password_not_same);
            return false;
        }
        if (!Utils.checkEmail(edtEmail.getText().toString())) {
            showTopTips(R.string.wrong_email);
            return false;
        }

        return true;
    }


    @Override
    public void setPresenter(RegisterContractV2.Presenter presenter) {
        this.presenter = presenter;
    }


    @Override
    public void registerFailed(int code, String msg) {
        showTopTips(msg);
    }

    @Override
    public void loginSuccess(User data) {
        jumpToActivity(AnchorStateActivity.class);
    }


}
